package tmhprediction.eval;

import java.util.HashMap;

public class SolTmhEvaluator {
	
	// Any protein with confidence sum >= cutoff is considered transmembran
	public static boolean cutoff(double cutoff, double confidenceSum){
		if (confidenceSum >= cutoff){
			return true;
		}
		return false;
	}
	
	// get statistics about beste cutoff
	public static void printStatistics(HashMap<String, double[]> result0){
		int count = 0;
        int countTMH = 0;
        double sum = 0;
        double min = Double.MAX_VALUE;
        double max = Double.MIN_VALUE;
        // the map contains #observed, #predicted #sum over tmh confidences
//        System.out.println(result0.size());
//        System.out.println("Sum of confidences, pred tmh count, real tmh count");
        for (String key : result0.keySet()) {
            double[] resultValues0 = result0.get(key);
            double predictionConf = resultValues0[2];
//            System.out.print(key);
//            System.out.print(" ");
//            System.out.print(predictionConf);
//            System.out.print(" ");
            double realTMHCount = resultValues0[0];
            double predTMHCount = resultValues0[1];
//            System.out.print(resultValues0[1]);
//            System.out.print(" ");
//            System.out.println(realTMHCount);
            if (predTMHCount > 0 && realTMHCount > 0) {
                if (predictionConf < min) {
                    min = predictionConf;
                }
                if (predictionConf > max) {
                    max = predictionConf;
                }
                sum = sum + predictionConf;
                countTMH++;
            }
            count++;
        }        
        double avg = sum / countTMH;
//        System.out.println("### Cutoff Statistics ###");
//        System.out.print("confidence sum: ");
//        System.out.println(sum);
//        System.out.print("tmh proteins: ");
//        System.out.println(countTMH);
//        System.out.print("Avg. confidence sum: ");
//        System.out.println(avg);
//        System.out.print("Max: ");
//        System.out.println(max);
//        System.out.print("Min: ");
//        System.out.println(min);
//        System.out.print("Count: ");
//        System.out.println(count);
        
	}
	
	public static int[][] confusionSVM1 (HashMap<String, double[]> result, double cutoff){
		int[][] confusionMatrix = new int[2][2];
		int TP = 0;
		int FP = 0;
		int TN = 0;
		int FN = 0;
		for (String key : result.keySet()) {
			double[] resultValues = result.get(key);
			double observed = resultValues[0];
//			double predicted = resultValues[1];
			double predictedConf = resultValues[2];
			if (observed > 0){
				if (cutoff(cutoff,predictedConf)){
					TP++;
				} else {
					FN++;
				}
			} else {
				if (!cutoff(cutoff,predictedConf)){
					TN++;
				} else {
					FP++;
					//System.out.println("protein id from FP"+key);
				}
			}
			
		}
		confusionMatrix[0][0] = TP;
		confusionMatrix[1][0] = FP;
		confusionMatrix[0][1] = FN;
		confusionMatrix[1][1] = TN;
		
		return confusionMatrix;
	}

}
